package com.example.orderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.orderassembly.controller.ServiceOrderDetailController;
import com.woniuxy.entity.GoodOrder;
import com.woniuxy.entity.ServiceOrder;
import com.example.orderassembly.mapper.ServiceOrderMapper;
import com.example.orderassembly.service.IServiceOrderService;
import com.woniuxy.form.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * <p>
 * 用户服务订单表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@Service
@SuppressWarnings("all")
public class ServiceOrderServiceImpl extends ServiceImpl<ServiceOrderMapper, ServiceOrder> implements IServiceOrderService {
    @Resource
    ServiceOrderMapper serviceOrderMapper;
    @Resource
    ServiceOrderDetailController serviceOrderDetailController;

    @Override
    public Boolean insertServiceOrder(InsertServiceOrder insertServiceOrder) {
        ServiceOrder serviceOrder = new ServiceOrder();
        BeanUtils.copyProperties(insertServiceOrder,serviceOrder);
        //todo JWT 获取用户Id
        Integer userId =insertServiceOrder.getServiceOrderUserId();
        serviceOrder.setServiceOrderUserId(userId);
        serviceOrder.setServiceOrderStatus(0);
        int insert = serviceOrderMapper.insert(serviceOrder);
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < insertServiceOrder.getSecondService().size(); i++) {
            map.put(insertServiceOrder.getSecondService().get(i),insertServiceOrder.getSecondServiceNum().get(i));
        }
        InsertServiceOrderDetail insertGoodOrderDetail = new InsertServiceOrderDetail();
        insertGoodOrderDetail.setServiceAttribute(map);
        insertGoodOrderDetail.setServiceOrderId(serviceOrder.getServiceOrderId());
        insertGoodOrderDetail.setUserId(insertServiceOrder.getServiceOrderUserId());
        serviceOrderDetailController.insertServiceOrderDetail(insertGoodOrderDetail);
        return insert==1;
    }

    @Override
    public Boolean updateServiceOrderStatus(Integer serviceOrderId, Integer status) {
        ServiceOrder serviceOrder = serviceOrderMapper.selectById(serviceOrderId);
        Integer serviceOrderStatus = serviceOrder.getServiceOrderStatus();
        UpdateWrapper<ServiceOrder> serviceOrderUpdateWrapper = new UpdateWrapper<>();
        if(serviceOrderStatus==3||serviceOrderStatus==2){
            return false;
        }
        if((serviceOrderStatus==0&&status==1)||(serviceOrderStatus==1&&status==3)||(serviceOrderStatus==1&&status==2)){
            serviceOrderUpdateWrapper.eq("service_order_id",serviceOrderId);
            serviceOrderUpdateWrapper.set("service_order_status",status);
            int update = serviceOrderMapper.update(null, serviceOrderUpdateWrapper);
            return update==1;
        }
        return false;
    }

    @Override
    public IPage<ServiceOrder> getServiceOrderByPlatform(GetOrder getOrder) {
        Page<ServiceOrder> goodOrderPage = new Page<>();
        goodOrderPage.setCurrent(getOrder.getIndexPage());
        goodOrderPage.setSize(getOrder.getPageSize());
        QueryWrapper<ServiceOrder> goodOrderQueryWrapper = new QueryWrapper<>();
        IPage<ServiceOrder> goodOrderIPage = null;
        if (getOrder.getStartDate()==null||getOrder.getEndDate()==null){
            goodOrderIPage = serviceOrderMapper.selectPage(goodOrderPage, null);
        }else {
            goodOrderQueryWrapper.between("service_order_create_time", getOrder.getStartDate(), getOrder.getEndDate());
            goodOrderIPage = serviceOrderMapper.selectPage(goodOrderPage, goodOrderQueryWrapper);
        }
        return goodOrderIPage;
    }

    @Override
    public IPage<ServiceOrder> getServiceOrderByStore(Integer storeId, GetOrder getOrder) {
        Page<ServiceOrder> goodOrderPage = new Page<>();
        goodOrderPage.setCurrent(getOrder.getIndexPage());
        goodOrderPage.setSize(getOrder.getPageSize());
        QueryWrapper<ServiceOrder> goodOrderQueryWrapper = new QueryWrapper<>();
        goodOrderQueryWrapper.eq("service_order_store_id",storeId);
        IPage<ServiceOrder> goodOrderIPage = null;
        if(getOrder.getEndDate()==null){
            goodOrderIPage = serviceOrderMapper.selectPage(goodOrderPage, goodOrderQueryWrapper);
        }else {
            goodOrderQueryWrapper.between("service_order_create_time", getOrder.getStartDate(), getOrder.getEndDate());
            goodOrderIPage = serviceOrderMapper.selectPage(goodOrderPage, goodOrderQueryWrapper);
        }
        return goodOrderIPage;

    }

    @Override
    public IPage<ServiceOrder> getServiceOrderByUserId(GetOrder getOrder) {
        Page<ServiceOrder> serviceOrderPage = new Page<>(getOrder.getIndexPage(), getOrder.getPageSize());
        QueryWrapper<ServiceOrder> serviceOrderQueryWrapper = new QueryWrapper<>();
        //todo 根据JWT 获取用户Id
        Integer userId = getOrder.getUserId();
        serviceOrderQueryWrapper.eq("service_order_user_id",userId);
        serviceOrderQueryWrapper.between(getOrder.getStartDate()!=null&&getOrder.getEndDate()!=null,"service_order_create_time",getOrder.getStartDate(),getOrder.getEndDate());
        IPage<ServiceOrder> iPage = serviceOrderMapper.selectPage(serviceOrderPage, serviceOrderQueryWrapper);
        return iPage;
    }

    @Override
    public ServiceOrder getServiceOrderById(Integer id) {
        ServiceOrder serviceOrder = getBaseMapper().selectById(id);
        return serviceOrder;

    }

    @Override
    public Boolean updateServiceOrderPayType(Integer orderId, Integer payType) {
        UpdateWrapper<ServiceOrder> serviceOrderUpdateWrapper = new UpdateWrapper<>();
        serviceOrderUpdateWrapper.eq("service_order_id",orderId).set("service_order_pay_type",payType);
        int update = getBaseMapper().update(null, serviceOrderUpdateWrapper);
        return update==1;
    }
}
